namespace SubSonic
{
    using SubSonic.Utilities;
    using System;
    using System.Data;

    [Serializable]
    public class Where
    {
        private string columnName;
        private SubSonic.Comparison comp;
        private WhereCondition condition;
        private System.Data.DbType dbType;
        private string parameterName;
        private object paramValue;
        private string tableName;

        public static string GetComparisonOperator(SubSonic.Comparison comp)
        {
            switch (comp)
            {
                case SubSonic.Comparison.NotEquals:
                    return " <> ";

                case SubSonic.Comparison.Like:
                    return " LIKE ";

                case SubSonic.Comparison.NotLike:
                    return " NOT LIKE ";

                case SubSonic.Comparison.GreaterThan:
                    return " > ";

                case SubSonic.Comparison.GreaterOrEquals:
                    return " >= ";

                case SubSonic.Comparison.LessThan:
                    return " < ";

                case SubSonic.Comparison.LessOrEquals:
                    return " <= ";

                case SubSonic.Comparison.Blank:
                    return " ";

                case SubSonic.Comparison.Is:
                    return " IS ";

                case SubSonic.Comparison.IsNot:
                    return " IS NOT ";
            }
            return " = ";
        }

        public static Where ParseExpression(string expression, WhereCondition condition)
        {
            SubSonic.Comparison blank = SubSonic.Comparison.Blank;
            Where where = null;
            int length = 0;
            string str = expression.ToLower();
            if (str.Contains("<>"))
            {
                blank = SubSonic.Comparison.NotEquals;
                length = str.IndexOf("<");
            }
            else if (str.Contains(">=") || str.Contains("> ="))
            {
                blank = SubSonic.Comparison.GreaterOrEquals;
                length = str.IndexOf(">");
            }
            else if (str.Contains("<=") || str.Contains("< ="))
            {
                blank = SubSonic.Comparison.LessOrEquals;
                length = str.IndexOf("<");
            }
            else if (str.Contains("<"))
            {
                blank = SubSonic.Comparison.LessThan;
                length = str.IndexOf("<");
            }
            else if (str.Contains(">"))
            {
                blank = SubSonic.Comparison.GreaterThan;
                length = str.IndexOf(">");
            }
            else if (str.Contains("="))
            {
                blank = SubSonic.Comparison.Equals;
                length = str.IndexOf("=");
            }
            else if (str.Contains(" not like "))
            {
                blank = SubSonic.Comparison.NotLike;
                length = str.IndexOf("not like");
            }
            else if (str.Contains(" like "))
            {
                blank = SubSonic.Comparison.Like;
                length = str.IndexOf("like");
            }
            else if (str.Contains(" is not "))
            {
                blank = SubSonic.Comparison.IsNot;
                length = str.IndexOf("is not");
            }
            else if (str.Contains(" is "))
            {
                blank = SubSonic.Comparison.Is;
                length = str.IndexOf("is");
            }
            if (blank != SubSonic.Comparison.Blank)
            {
                string pattern = expression.Substring(0, length).Trim();
                string comparisonOperator = GetComparisonOperator(blank);
                string str4 = Utility.FastReplace(Utility.FastReplace(expression, pattern, string.Empty, StringComparison.InvariantCultureIgnoreCase), comparisonOperator.Trim(), string.Empty, StringComparison.InvariantCultureIgnoreCase).Trim();
                where = new Where();
                where.ColumnName = pattern;
                where.Comparison = blank;
                where.condition = condition;
                where.ParameterValue = str4;
            }
            return where;
        }

        public string ColumnName
        {
            get
            {
                return this.columnName;
            }
            set
            {
                this.columnName = value;
            }
        }

        public SubSonic.Comparison Comparison
        {
            get
            {
                return this.comp;
            }
            set
            {
                this.comp = value;
            }
        }

        public WhereCondition Condition
        {
            get
            {
                return this.condition;
            }
            set
            {
                this.condition = value;
            }
        }

        public System.Data.DbType DbType
        {
            get
            {
                return this.dbType;
            }
            set
            {
                this.dbType = value;
            }
        }

        public string ParameterName
        {
            get
            {
                return (this.parameterName ?? this.ColumnName);
            }
            set
            {
                this.parameterName = value;
            }
        }

        public object ParameterValue
        {
            get
            {
                return this.paramValue;
            }
            set
            {
                this.paramValue = value;
            }
        }

        public string TableName
        {
            get
            {
                return this.tableName;
            }
            set
            {
                this.tableName = value;
            }
        }

        public enum WhereCondition
        {
            AND,
            OR
        }
    }
}

